package com.zmkj.taobao.taobaoorderservice.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.zmkj.taobao.taobaoorderservice.entity.Order;
import com.zmkj.taobao.taobaoorderservice.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/OrderController")
public class OrderController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    EurekaClient eurekaClient;

    @RequestMapping("/OrderList")
    public List OrderList(){
        List<Order> orderList = new ArrayList<>();
        Order order1 = new Order(1,"13",12.3,"gjs");
        Order order2 = new Order(2,"14",13.3,"gjl");
        Order order3 = new Order(3,"15",14.3,"yjy");
        Order order4 = new Order(4,"16",15.3,"zds");
        orderList.add(order1);
        orderList.add(order2);
        orderList.add(order3);
        orderList.add(order4);
        return orderList;
    }

    @RequestMapping("/queryOrderByid/{orderId}")
    public Order queryOrderByid(@PathVariable(value = "orderId") Integer orderId){
        Order orderInfo = new Order(1,"13",12.3,"gjs");
                                            //第一个参数 {调取服务的名称}       第二个参数 {是不是https请求  用http请求 写false}
        //获取服务信息
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("TAOBAO-PRODUCTSERVICE",false);
        String url = instanceInfo.getHomePageUrl();
        //调用服务接口获取返回数据
        Object data = restTemplate.getForObject(url+"/ProductController/selectById/"+orderId,Object.class);
        List<Product> productList = (List<Product>) data;
        orderInfo.setProductList(productList);

        return orderInfo;
    }

}
